package com.android.sharecar.activity;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import android.app.AlertDialog;
import android.app.ListActivity;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.drawable.BitmapDrawable;
import android.os.Bundle;
import android.util.Log;
import android.view.GestureDetector;
import android.view.GestureDetector.OnGestureListener;
import android.view.LayoutInflater;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnTouchListener;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.TextView;

import com.android.sharecar.model.User;
import com.android.sharecar.model.UserTalkMsg;
import com.android.sharecar.util.BitmapUtil;

/**
 * TalkListActivity shows user's public talk..
 * 
 * @author xinyan
 * @date 2011-10-28
 */
public class TalkListActivity extends ListActivity implements OnTouchListener,
		OnGestureListener {
	public static final String TAG = "TalkListActivity";

	// views
	private Button mPostATalk, mRefresh;

	private List<UserTalkMsg> mTalks;

	// Gesture detector that used to switch activity by gesture.
	private GestureDetector mGestureDetector;

	private static int mVerticalMinDistance = 70;

	private static int mMinVelocity = 0;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.list_talk);

		setTitlebar();
		findViews();
		setListenerForViews();

		getLatestTalksFromServer();

		// why we don't need to judge whether mtalks is null?
		// i think android will take care of this, remember android:id/empty....
		setListAdapter(new TalkListAdapter(this, R.layout.list_item_talk,
				mTalks));

		mGestureDetector = new GestureDetector(this);
		LinearLayout rootLayout = (LinearLayout) findViewById(R.id.list_talk_rootLayout);
		rootLayout.setOnTouchListener(this);
		rootLayout.setLongClickable(true);
	}

	@Override
	protected void onResume() {
		super.onResume();

	}

	private void setTitlebar() {
		/*
		 * MainTabWidgetActivity.mInstance.getWindow().setFeatureInt(
		 * Window.FEATURE_CUSTOM_TITLE, R.layout.titilebar_talklist);
		 */

		mRefresh = (Button) findViewById(R.id.talkListActivity_btn_refresh);
		mPostATalk = (Button) findViewById(R.id.talkListActivity_btn_postATalk);

		mPostATalk.setOnClickListener(new View.OnClickListener() {

			@Override
			public void onClick(View v) {
				LayoutInflater factory = LayoutInflater
						.from(TalkListActivity.this);
				final View replyView = factory.inflate(
						R.layout.list_item_reply, null);

				new AlertDialog.Builder(TalkListActivity.this)
						.setTitle("请输入发表内容")
						.setView(replyView)
						.setPositiveButton(R.string.positive,
								new DialogInterface.OnClickListener() {

									@Override
									public void onClick(DialogInterface dialog,
											int which) {
										EditText TalkText = (EditText) replyView
												.findViewById(R.id.talkList_et_reply);

										// Get the text and do something.
									}
								})
						.setNegativeButton(R.string.cancel,
								new DialogInterface.OnClickListener() {

									@Override
									public void onClick(DialogInterface dialog,
											int which) {
										dialog.dismiss();
									}
								}).create().show();
			}
		});
	}

	private void findViews() {
		/*
		 * LayoutInflater inflater = LayoutInflater
		 * .from(MainTabWidgetActivity.mInstance); View titleBar =
		 * inflater.inflate(R.layout.titilebar_talklist, null);
		 */

	}

	private void setListenerForViews() {

	}

	private void getLatestTalksFromServer() {

		// i think we just get the other user's talk that around current user
		// is enough.
		UserTalkMsg msg;
		User user;
		Bitmap headPic;
		mTalks = new ArrayList<UserTalkMsg>();
		BitmapDrawable bitmapDrawable = (BitmapDrawable) getResources()
				.getDrawable(R.drawable.myheadpic);
		headPic = bitmapDrawable.getBitmap();

		for (int i = 0; i < 8; i++) {
			user = new User();
			msg = new UserTalkMsg();

			user.setNickName("用户" + i);
			user.setHeadPicture(BitmapUtil.getBytes(headPic));

			msg.setUser(user);
			msg.setCommentNum(3);
			msg.setContent("这只是一个静态生成的发言、、哦哦");
			msg.setPostTime(new Date());

			mTalks.add(msg);
		}
	}

	@Override
	protected void onListItemClick(ListView l, View v, int position, long id) {
		super.onListItemClick(l, v, position, id);
		Intent intent = new Intent();
		intent.setClass(this, TalkListCommentsActivity.class);
		startActivity(intent);
	}

	private final class TalkListAdapter extends BaseAdapter {
		private final Context mContext;

		private final Integer mResource;

		private final List<UserTalkMsg> mData;

		public TalkListAdapter(Context context, Integer resource,
				List<UserTalkMsg> value) {
			this.mContext = context;
			this.mResource = resource;
			this.mData = value;
		}

		@Override
		public int getCount() {
			return mData.size();
		}

		@Override
		public Object getItem(int position) {
			return mData.get(position);
		}

		@Override
		public long getItemId(int position) {
			return position;
		}

		@Override
		public View getView(int position, View convertView, ViewGroup parent) {
			Log.i(TAG, "getView-----" + position + "---convertView--"
					+ convertView);
			TextView nickName, content, postTime, commentNum;
			View view = View.inflate(mContext, mResource, null);
			ImageView headPic = (ImageView) view
					.findViewById(R.id.talkListActivity_iv_headPic);
			/*
			 * nickName = (TextView) view
			 * .findViewById(R.id.reserveRequestList_tv_nickName); gender =
			 * (TextView) view .findViewById(R.id.reserveRequestList_tv_gender);
			 */
			nickName = (TextView) view
					.findViewById(R.id.talkListActivity_tv_nickName);
			content = (TextView) view
					.findViewById(R.id.talkListActivity_tv_content);
			postTime = (TextView) view
					.findViewById(R.id.talkListActivity_tv_postTime);
			commentNum = (TextView) view
					.findViewById(R.id.talkListActivity_tv_commentNum);

			UserTalkMsg msg = mData.get(position);
			if (null != msg) {
				User user = msg.getUser();
				if (null != user) {
					headPic.setImageBitmap(BitmapUtil.getBitmap(user
							.getHeadPicture()));
					nickName.setText(user.getNickName());
					/* gender.setText(user.getGender()); */
				}
				content.setText(msg.getContent());
				postTime.setText(msg.getPostTime().toLocaleString());
				commentNum.setText(String.valueOf(msg.getCommentNum()));
			}

			return view;
		}

	}

	@Override
	public boolean onDown(MotionEvent e) {
		return false;
	}

	@Override
	public void onShowPress(MotionEvent e) {

	}

	@Override
	public boolean onSingleTapUp(MotionEvent e) {
		return false;
	}

	@Override
	public boolean onScroll(MotionEvent e1, MotionEvent e2, float distanceX,
			float distanceY) {
		return false;
	}

	@Override
	public void onLongPress(MotionEvent e) {

	}

	@Override
	public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX,
			float velocityY) {
		if (e1.getX() - e2.getX() > mVerticalMinDistance
				&& Math.abs(velocityX) > mMinVelocity) {

			// 向左手势

			Intent intent = new Intent(this, OnlineUserListActivity.class);
			startActivity(intent);
		} else if (e2.getX() - e1.getX() > mVerticalMinDistance
				&& Math.abs(velocityX) > mMinVelocity) {

			// 向右手势
			Intent intent = new Intent(this,
					ReserveRequestListTabWidgetActivity.class);
			startActivity(intent);
		}

		return false;
	}

	@Override
	public boolean onTouch(View v, MotionEvent event) {
		return false;
	}

	@Override
	public boolean dispatchTouchEvent(MotionEvent ev) {
		mGestureDetector.onTouchEvent(ev);
		return super.dispatchTouchEvent(ev);
	}

}
